package com.wtwd.campus.method;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.wtwd.campus.common.RespCode;
import com.wtwd.campus.entity.VerifyResult;
import com.wtwd.campus.service.RedisService;
import com.wtwd.campus.utils.Sha256Utils;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.TreeMap;

/**
 * @Author ldaoliang
 * @Date create in 10:30 2021/7/5
 * @Description
 */
public class QueryEncrypt implements EncryptVerify {
    @Override
    public VerifyResult verify(String sign, String token, HttpServletRequest request, RedisService redisService) {
        Enumeration<String> parameterNames = request.getParameterNames();
        TreeMap<String, String> treeMap = new TreeMap();
        while (parameterNames.hasMoreElements()) {
            String name = parameterNames.nextElement();
            treeMap.put(name, request.getParameter(name));
        }
        // 转成jsonObject
        JSONObject json = JSONObject.parseObject(JSONObject.toJSONString(treeMap), Feature.OrderedField);
        if (!sign.equalsIgnoreCase(Sha256Utils.SHA256(token + json.toJSONString()))) {
            VerifyResult verifyResult = new VerifyResult();
            verifyResult.setResult(false);
            verifyResult.setRespCode(RespCode.REQUEST_ILLEGAL);
            return verifyResult;
        }
        return null;
    }

    public static void main(String[] args) {
//        25abb4a49829c652fc273aafa1ba256409973e9e26bb49260f91ae07ff60740c
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("imei","86624");
        jsonObject.put("currPage",1);
        jsonObject.put("pageSize",10);
        String s = Sha256Utils.SHA256("03b0a5aa62e14d64a3836264f8052fc7" + jsonObject.toJSONString());
        System.out.println(s);
    }
}
